﻿using System;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 这个类
/// </summary>
public class UIManager : Singleton<UIManager>
{

    GameObject _uiRoot;

    // 层次， 根节点
    Dictionary<UILayer, GameObject> _uiLayerRoot = new Dictionary<UILayer, GameObject>();

    public void Init()
    {
        /// 加载
        if (_uiRoot == null)
        {
            _uiRoot = ResMgr.instance.GetInstance("UI/UISystem");
            GameObject.DontDestroyOnLoad(_uiRoot);

        }
        // UI分5层
        _uiLayerRoot.Add(UILayer.Scene, _uiRoot.Find<Transform>("Scene").gameObject);
        _uiLayerRoot.Add(UILayer.Touch, _uiRoot.Find<Transform>("Touch").gameObject);
        _uiLayerRoot.Add(UILayer.FightUI, _uiRoot.Find<Transform>("FightUI").gameObject);
        _uiLayerRoot.Add(UILayer.Normal, _uiRoot.Find<Transform>("Normal").gameObject);
        _uiLayerRoot.Add(UILayer.Top, _uiRoot.Find<Transform>("Top").gameObject);

    }

    //添加某个界面上的某个ui 路径 和 加到哪一个层上
    public GameObject Add(string uiPath, UILayer layer)
    {
        
        var root = ResMgr.instance.GetInstance(uiPath);
        // 添加父节点
        root.transform.SetParent(_uiLayerRoot[layer].transform, false);

        return root;

    }

    public void RemoveLayer(UILayer layer = UILayer.Normal)
    {
        _uiLayerRoot[layer].DestoryAllChildren();
    }

    public GameObject Replace(string uiPath, UILayer layer = UILayer.Normal)
    {

        RemoveLayer(layer);

        return Add(uiPath, layer);
    }

    //删除某个界面上的某个ui

    public void Remove(GameObject ui)
    {

        ResMgr.instance.Release(ui);

    }

}
// UI层次分5层
public enum UILayer
{
    Scene,
    Touch,
    FightUI,
    Normal,
    Top
}

